package com.jt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 金帮嘉诚 @date 2021-08-18 星期三 20:21
 */
@EnableFeignClients
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalancerRestTemplate(){
        return new RestTemplate();
    }

    @RestController
    @RequestMapping("/demo")
    public class DemoController{

        @Autowired
        private RestTemplate loadBalancerRestTemplate;

        /**
         * 使用RestTemplate对象 @LoadBalanced注解 发送请求
         * url:http://localhost:8091/demo/echo4/{msg}
         * */
        @GetMapping("/echo4/{msg}")
        public String echo4(@PathVariable("msg") String msg){
            String serviceId = "sca-providerre";
            String url = String.format("http://%s/provider/echo/%s",serviceId,msg);
            return loadBalancerRestTemplate.getForObject(url,String.class);

        }

    }




}
